<?php
/**
 * ClassName:货代处理器
 * @auditor leijunxiang
 * @time 2025/4/1 11:40
 */

namespace app\tool\freightForward;

use app\model\freightForward\FreightForward;
use app\modelOperate\freightForward\FreightForwardOpe;
use app\tool\ToolBase;

class FreightForwardHandle extends ToolBase
{

    /**
     * (true/false)开始新增(货代名称)
     */
    public function startAdd($name)
    {
        $this->init();
        $operate = app(FreightForwardOpe::class);
        $obj = FreightForward::getObj();
        $obj->setName($name);
        $flag = $operate->saveData($obj->getAttribute());
        if ($flag) {
            $this->setErrCode(SUCCESS);
            $this->setErrMsg("新增成功");
        } else {
            $this->setErrCode(FAIL);
            $this->setErrMsg("新增失败");
        }
        return $this->isGoOn();
    }


    /**
     * (数据)开始查询
     */
    public function startSelect()
    {
        $this->init();
        $operate = app(FreightForwardOpe::class);
        //查询数据
        $data = $operate->selectData();
        return $data;
    }

    /**
     * (true/false)开始删除(货代id数组)
     */
    public function startDel($arr)
    {
        $this->init();
        $operate = app(FreightForwardOpe::class);
        try {
            if ($arr) {
                foreach ($arr as $item) {
                    $flag = $operate->deleteForward($item);
                    if (!$flag) {
                        $this->setErrCode(FAIL);
                        $this->setErrMsg("删除失败");
                        break;
                    }
                }
            }

        } catch (\Exception $e) {
            $this->errCode = FAIL;
            $this->errMsg = $e->getMessage();
        }
        return $this->isGoOn();
    }

    /**
     * 开始修改(货代id,名称)
     */
    public function startUpdate($forwardId, $name)
    {
        $this->init();
        $operate = app(FreightForwardOpe::class);
        try {
            $flag = $operate->updateForward($forwardId, $name);
            if ($flag) {
                $this->setErrCode(SUCCESS);
                $this->setErrMsg("修改成功");
            } else {
                $this->setErrCode(FAIL);
                $this->setErrMsg("修改失败");
            }
        } catch (\Exception $e) {
            $this->errCode = FAIL;
            $this->errMsg = $e->getMessage();
        }
        return $this->isGoOn();
    }


}